package com.biz.primus.model.promotion.vo;

import com.biz.primus.model.promotion.enums.PromotionTargetLimitTypeEnum;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

/**
 * 折扣促销Vo
 *
 * @author david-liu
 */
@ApiModel(value = "折扣促销Vo")
@Setter
@Getter
public class DiscountPromotionVo extends BasePromotionVo {
    private static final long serialVersionUID = -7005332458636717857L;

    @ApiModelProperty(value = "是否阶梯折扣")
    private Boolean isStairCut = false;

    @ApiModelProperty(value = "阶梯数量")
    private Integer stairCount;

    @ApiModelProperty(value = "按照商品金额/商品数量折扣", allowableValues = "DISCOUNT_BY_PRODUCT_AMOUNT, DISCOUNT_BY_PRODUCT_QUANTITY")
    private PromotionTargetLimitTypeEnum limitType;

    @ApiModelProperty(value = "折扣率")
    private List<DiscountRateVo> discountRates;

    public Map<Integer, BigDecimal> toRateMap() {
        Map<Integer, BigDecimal> ret = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(this.discountRates)) {
            discountRates.forEach(rate -> {
                ret.put(rate.getRateCondition(), rate.getDiscountRate());
            });
        }
        return ret;
    }

    public void mapIntoRates(Map<Integer, BigDecimal> rates) {
        List<DiscountRateVo> rateVos = Lists.newArrayList();
        if (MapUtils.isNotEmpty(rates)) {
            rates.forEach((k, v) -> rateVos.add(new DiscountRateVo(k, v)));

        }
        this.discountRates = rateVos;
    }

}
